angular.module('MyApp')
    .factory('user', function() {
        return {
            name: '',
            password: '',
            email: '',
            age: 0,
            sex: 'male',
            email: '',
            errors: {},
            validate: function () {
                var result = true;
                this.errors = {};

                // invalid email format , the value is reset
                // by angular form validation of input[email] direcive
                if (typeof this.email !== 'string') {
                    this.errors.email = 'Email is not valid';
                } else if (this.email.length < 8) {
                    this.errors.email = 'Email must be 8 min';
                    result = false;
                }

                if (typeof this.password !== 'string') {
                    this.errors.password = 'Enter valid password';
                } else if (this.password.length < 5) {
                    this.errors.password = 'Password must be 5 min';
                    result = false;
                }

                return result;
            }
        }
    })